﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using EShop.Common;
using EShop.BussinessObjects;
using MySql.Data.MySqlClient;

namespace EShop.DataAccess
{
    public class CDAProductExtra
    {

        public static List<ProductExtra> Select(ProductExtra productExtra)
        {
            MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
            con.Open();


            string _cmdText =
                 "SELECT pe.* " +
                 "FROM vw_ProductExtra pe " +
                 "WHERE ((pe.ID = " + productExtra.ID.ToString() + ") OR (" + productExtra.ID.ToString() + " = " + ((int)cnstDatabase.DBOperationResult.ValueIsNotEffective).ToString() + "))" +
                 "ORDER BY pe.ID";
            
            MySqlCommand cmd = new MySqlCommand(_cmdText, con);
            MySqlDataAdapter dataAdapter = new MySqlDataAdapter(cmd);
            DataTable tbl = new DataTable();
            dataAdapter.Fill(tbl);

            List<ProductExtra> _productExtraList = new List<ProductExtra>();

            foreach (DataRow row in tbl.Rows)
            {
                ProductExtra _productExtra = new ProductExtra();
                _productExtra.ID = (row["ID"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue) : Convert.ToInt32(row["ID"]));
                
                _productExtra.Product = new Product();
                _productExtra.Product.ID = (row["ProductID"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue) : Convert.ToInt32(row["ProductID"]));
                
                _productExtra.Ingredient = new Ingredient();
                _productExtra.Ingredient.ID = (row["IngredientID"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue) : Convert.ToInt32(row["IngredientID"]));
                _productExtra.Ingredient.Name = (row["IngredientName"].ToString() == "" ? null : Convert.ToString(row["IngredientName"]));

                _productExtra.Price = (row["Price"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue) : Convert.ToDecimal(row["Price"]));
                _productExtra.IsAdditional = (row["IsAdditional"].ToString() == "1" ? true : false);

                _productExtraList.Add(_productExtra);
            }

            con.Close();
            return _productExtraList;
        }
    }
}